<?php

namespace App\Http\Controllers\Guard\User;

use App\Exports\DriverExport;
use App\Exports\GoExport;
use App\Exports\UserExport;
use App\Http\Controllers\Common\GuardController;
use App\Models\Cert\CertDriver;
use App\Models\Cert\CertGo;
use App\Models\SerialNumber\SerialNumber;
use App\Models\User\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;

class UserController extends GuardController {

    public function lst(Request $request) {
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',20);
        $conditions = $request->input();
        //dd($conditions);
        $records = (new User())->getLst($conditions)
            ->with(['getDriverInfo'])
            ->paginate($pageSize,['*'],'page',$page);
        return responseSuccess($records->items(),$records->total());
    }

    public function add(Request $request) {
        $conditions = $request->input();
        $id = $request->input('id',0);
        //dd($conditions);
        $user = User::updateOrCreate(compact('id'),$conditions);
        return responseSuccess($user->toArray());
    }

    public function detail(Request $request) {
        $ids = $request->input('ids','');
        return $this->getRecordByIds(User::class, $ids);
    }

    public function del(Request $request) {
        $ids = $request->input('ids','');
        $user = User::where(['id'=>$ids])->first();

        try {
            DB::beginTransaction();
            $serialNumber = SerialNumber::where(['user_id'=>$user->id])->first();
            if(!empty($serialNumber)){
                $serialNumber->user_id = 0;
                $serialNumber->save();
            }
            $user->forceDelete();
            DB::commit();
            return responseSuccess();
        }catch (\Exception $e){
            DB::rollBack();
            $tip = $e->getFile().':'.$e->getLine().':'.$e->getMessage();
            return responseFail($tip);
        }
    }

    public function exportXls(Request $request) {
        $conditions = $request->input();
        return Excel::download(new UserExport($conditions), 'users.xlsx');
    }

    public function resetPassword(Request $request) {
        $id = $request->input('id');
        $password = 'anquanbao@163';
        User::where(compact('id'))->update(['password'=>md5($password)]);
        return responseSuccess([],0,"重置密码成功，新密码：$password");
    }


    public function driverLst(Request $request) {
        $conditions = $request->input();
        $pageSize = $request->input('pageSize',30);
        $page = $request->input('page',1);
        $records = (new CertDriver())->getLst($conditions)->with(['getUser'])
            ->paginate($pageSize,['*'],'page',$page);
        return responseSuccess($records->items(),$records->total());
    }

    public function driverExportXls(Request $request) {
        $conditions = $request->input();
        return Excel::download(new DriverExport($conditions), 'driver.xlsx');
    }

    public function goLst(Request $request) {
        $conditions = $request->input();
        $pageSize = $request->input('pageSize',30);
        $page = $request->input('page',1);
        $records = (new CertGo())->getLst($conditions)->with(['getUser','getGoOther'])
            ->paginate($pageSize,['*'],'page',$page);
        return responseSuccess($records->items(),$records->total());
    }

    public function goExportXls(Request $request) {
        $conditions = $request->input();
        return Excel::download(new GoExport($conditions), 'go.xlsx');
    }




}
